//
//  ByApiUser.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/18.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByApiUser.h"

@implementation ByApiUser
/** 发送验证码 */
+ (void)getCode:(ByNetParamsModel *)model
        success:(void(^)(ByNetBaseResult *result))success
        failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_SecurityCode_send";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.mobile forKey:@"mobile"];
    [params setObject:model.type forKey:@"code_type"];
    [params setObject:@"+86" forKey:@"country"];
    [params setObject:@"alert" forKey:@"send_type"];
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           // 若返回数据为字典类型则抛出异常
           NSAssert([responseObject isKindOfClass:[NSString class]], @"请求验证码 返回数据是字符串类型的，请检查");
           
           CodeLog(@"******** %@ 获取验证码成功: %@", apiType, responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = responseObject;
           result.data = responseObject; // 验证码（6位字符串）
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           CodeLog(@"******** %@ 获取验证码失败: %@", apiType, error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
    
}


/** 登陆 */
+ (void)login:(ByNetParamsModel *)model
      success:(void(^)(ByNetBaseResult *result))success
      failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_UserAccount_loginByMobile";
    static NSInteger apiVersion = 100;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%.0lf%d", [NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.username forKey:@"mobile"];
    [params setObject:model.password forKey:@"password"];
    [params setObject:@"itboye" forKey:@"code"];
    [params setObject:@"role_user" forKey:@"role"];
    [params setObject:@"+86" forKey:@"country_no"];
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeDictionary
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           CodeLog(@"******** 登录 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"登录成功";
           ByModelUser *model = [[ByModelUser alloc] initContentWithDic:responseObject];
           result.data = model;
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           CodeLog(@"******** 登录 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
    
}

/** 注册 */
+ (void)reigster:(ByNetParamsModel *)model
         success:(void(^)(ByNetBaseResult *result))success
         failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_UserAccount_register";
    static NSInteger apiVersion = 100;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.username forKey:@"mobile"];
    [params setObject:model.password forKey:@"password"];
    [params setObject:@"+86" forKey:@"country_no"];
    [params setObject:model.code forKey:@"code"];
//    [params setObject:model.from forKey:@"reg_from"];
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           CodeLog(@"******** 注册 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"注册成功";
           result.data = responseObject;
           result.notify_id = notifyID;
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           CodeLog(@"******** 注册 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
}


@end
